<?php View::display('accounting/menu');?>
<header class="head">
	<!-- ."main-bar -->
	<div class="main-bar">
		<div class="container-fluid">
			<div class="row-fluid">
				<div class="span12">
					<h3>
						<i class="icon-wrench"></i> การแจ้งชำระ
					</h3>
				</div>
			</div>
			<!-- /.row-fluid -->
		</div>
		<!-- /.container-fluid -->
	</div>
	<!-- /.main-bar -->
</header>
<!-- /.head -->
<!-- #content -->
<div id="content">
	<!-- .outer -->
	<div class="container-fluid outer">
	
		<div class="row-fluid">
			<!-- .inner -->
			<div class="span12 inner">
				<!--Begin Datatables-->
				<div class="row-fluid">
					<div class="span12">
						<div class="box">
							<div class="body">
								เลือกดูของ
								<select class="selectShow">
									<?php $typeShow = isset($_GET['mode'])?$_GET['mode']:'daily' ;?>
									<option value="index.php?page=accounting/payment&mode=daily" <?php echo $typeShow=='daily'? 'selected': ''; ?>>วันนี้</option>
									<option value="index.php?page=accounting/payment&mode=monthly" <?php echo $typeShow=='monthly'? 'selected': ''; ?>>เดือนนี้</option>
									<option value="index.php?page=accounting/payment&mode=yearly" <?php echo $typeShow=='yearly'? 'selected': ''; ?>>ปีนี้</option>
								</select>
							</div>
						</div>
						<div class="box">
							<header>
								<h5>การแจ้งชำระ</h5>
							</header>
							<div class="body">
								<table class="table table-bordered dataTable responsive">
									<thead>
										<tr>
											<tr>
												<th>type</th>
												<th>account</th>
												<th>package</th>
												<th>bank</th>
												<th>date</th>
												<th>time</th>
												<th>price</th>
												<th></th>
											</tr>
										</tr>
									</thead>
									<tbody>
										<?php
										foreach($params['payments'] as $key => $row){
											$invs = array();
											foreach($row['invoices'] as $key => $value){
												$invs[] = '<a target="_blank" href="old.php?page=invoice&id='.$value['id'].'">'.$value['package']['type'].'-'.$value['package']['name'].'</a>'."</small>";
											}
											$invIds = '<ul class="unstyled">'. '<li>'.implode('</li><li>',$invs).'</li>'.'</ul>';
											$money = Helper::formatMoney($row['money'], false).' บาท';
											$payDate = new DateTime($row['payDate']);
											$payDate = $payDate->format('j/n/Y');
											$createTime = new DateTime($row['createTime']);
											$createTime = $createTime->format('j/m/Y');
											$slip = empty($row['slipPath'])? '':<<<HTML
											<p class="h-slip" style="position: relative; padding: 0;">
												<img src="images/slip_icon.jpg" width="32" height="32" style="position: absolute; left: 0; top: 0; pointer-events: none;" />
												<a target="_blank" href="slips/{$row['slipPath']}" style="opacity: 0;"><img src="slips/{$row['slipPath']}" width="32" height="32" /></a>
											</p>
HTML;
											$taxInvoice = $row['taxInvoice'] == 0? '': <<<HTML
											<p class="alert">ใบกำกับภาษี</p><br />
HTML;
											$buttonPay = "";
											foreach($row['invoices'] as $key => $value){
												if($value['status']!=Invoice::STATUS_PAY) {
													$buttonPay = <<<HTML
													<p><button class="payButton btn btn-info" aid={$row['id']}>จ่ายแล้ว</button></p>
HTML;
													continue;
												}
											}
											
											$createTime = new DateTime($row['createTime']);
											
											if($createTime->format('Y-m-d') == date('Y-m-d')) $type = 'Daily';
											else if($createTime->format('Y-m') == date('Y-m')) $type = 'Monthly';
											else if($createTime->format('Y') == date('Y')) $type = 'Yearly';
											else if($type) 'Previous year';
											
											$payTime = new DateTime($row['payTime']);
											$payTime = $payTime->format('H:i');
											
										echo <<<HTML
											<tr class="list-row" rowId="{$row['id']}">
												<td class="type">{$type}</td>
												<td class="account">
													<a target="_blank" href="old.php?page=accounting/account/detail&id={$row['account']['id']}">{$row['account']['email']}</a><br />
													tel: {$row['account']['phone']}
												</td>
												<td class="invoice-id">{$invIds}</td>
												<td class="bank">{$row['payBank']}</td>
												<td class="date">
													{$payDate}
												</td>
												<td>{$payTime}</td>
												<td>{$money}</td>
												<td>
													<div class="btn-group">
														<a class="btn dropdown-toggle action" data-toggle="dropdown" href="#">
															Action
															<span class="caret"></span>
														</a>
														<ul class="dropdown-menu">
															<!--<li><a class="paid" href="#">จ่ายแล้ว</a></li>-->
															<li><a class="paidTechnic" href="#">จ่ายแล้ว</a></li>
															<li><a class="paidMiss" href="#">ไม่พบการชำระเงิน</a></li>
															<li><a class="paidNotFully" href="#">ชำระเงินไม่ครบ</a></li>
															<!--
															<li class="divider"></li>
															<li><a target="_blank" href="old.php?page=invoice&id={$row['id']}">รายละเอียด</a></li>
															-->
														</ul>
													</div>
													<!--
													{$buttonPay}
													<p><button class="deltButton btn btn-danger" aid={$row['id']}>ลบ</button></p>
													-->
												</td>
											</tr>
HTML;
										}?>
									</tbody>
								</table>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
		<!--Begin Datatables-->
	</div>
</div>
<!-- /#content -->

<script type="text/template" class="modalText">
	<div class="modal hide fade">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
			<h3>Edit package id <span class="packageId"></span></h3>
		</div>
		<div class="modal-body">
			<form class="formAccount">
				<label>ชื่อ</label>
				<input type="text" class="name" value="" />
				<label>รายละเอียด</label>
				<textarea type="text" class="info"></textarea>
				<label>ราคา</label>
				<fieldset class="prices-fieldset"></fieldset>
				<button class="btn addPrice">เพิ่มราคา/เดือน</button>
			</form>
		</div>
		<div class="modal-footer">
			<a href="#" class="btn cancel">Cancel</a>
			<a href="#" class="btn btn-primary saveChange">Save changes</a>
		</div>
	</div>
</script>
<script type="text/template" class="modalAdd">
	<div class="modal hide fade">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
			<h3>Edit package id <span class="packageId"></span></h3>
		</div>
		<div class="modal-body">
			<form class="formAccount">
				<label>ประเภท</label>
				<select class="type">
					<option value="colo">colo</option>
					<option value="vps">vps</option>
					<option value="hosting">hosting</option>
				</select>
				<label>ชื่อ</label>
				<input type="text" class="name" value="" />
				<label>รายละเอียด</label>
				<textarea type="text" class="info"></textarea>
				<label>ราคา</label>
				<fieldset class="prices-fieldset"></fieldset>
				<button class="btn addPrice">เพิ่มราคา/เดือน</button>
			</form>
		</div>
		<div class="modal-footer">
			<a href="#" class="btn cancel">Cancel</a>
			<a href="#" class="btn btn-primary Add">Add</a>
		</div>
	</div>
</script>
<script type="text/template" class="modalPaidMiss">
<div class="modal hide fade">
	<div class="modal-header">
		<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
		<h3>Edit package id <span class="packageId"></span></h3>
	</div>
	<div class="modal-body">
		<form class="formPaidMiss">
			<label>จำนวนเงินที่ชำระ(จริง)</label>
			<input type="text" class="payTrue" value="" />
			<label>จำนวนเงินที่ขาด</label>
			<input type="text" class="moneyMiss" value="" />
			<label>รายละเอียดเพิ่มเติม</label>
			<textarea type="text" class="info"></textarea>
		</form>
	</div>
	<div class="modal-footer">
		<a href="#" class="btn cancel">Cancel</a>
		<button class="btn btn-primary submit">Submit</button>
	</div>
</div>
</script>
<script type="text/javascript" src="js/photoZoom.min.js"></script>
<script type="text/javascript">
$(function(){
	$('.selectShow').change(function(event){
		href = $(this).val();
		location.href = href;
	});
	
	$('.list-row').delegate('.yes', 'click', function(event){
		var dTarget = $(event.delegateTarget);
		var id = dTarget.attr('rowId');
		$('.deltButton', dTarget).button('loading');
		$.ajax('old.php?page=accounting/payment.rest', {
			type: 'DELETE',
			data: {id: id},
			dataType: 'JSON',
			success: function(data){
				$('.deltButton', dTarget).button('reset');
				if(data.success){
					dTarget.fadeOut('slow', function(){ $(this).remove(); });
				}
			}
		});
		$('.deltButton', dTarget).popover('hide');
	})
	.delegate('.paid', 'click', function(event){
		event.preventDefault();
		var ans = window.confirm('จ่ายแล้ว?');
		if(ans == false){
			return;
		}
		var dTarget = $(event.delegateTarget);
		var id = dTarget.attr('rowId');
		$('.action', dTarget).button('loading');
		$.ajax('old.php?page=accounting/payment.rest', {
			type: 'OPTIONS',
			data: {id: id, operation: 'payInvoice'},
			dataType: 'JSON',
			success: function(data){
				$('.action', dTarget).button('reset');
				if(data.success){
					delRow(dTarget);
				}
			}
		});
	})
	.delegate('.paidTechnic', 'click', function(event){
		event.preventDefault();
		var ans = window.confirm('จ่ายแล้ว + แจ้งติดตั้ง?');
		if(ans == false){
			return;
		}
		var dTarget = $(event.delegateTarget);
		var id = dTarget.attr('rowId');
		$('.action', dTarget).button('loading');
		$.ajax('old.php?page=accounting/payment.rest', {
			type: 'OPTIONS',
			data: {id: id, operation: 'paidTechnic'},
			dataType: 'JSON',
			success: function(data){
				$('.action', dTarget).button('reset');
				if(data.success){
					delRow(dTarget);
				}
			}
		});
	})
	.delegate('.paidMiss', 'click', function(event){
		event.preventDefault();
		var ans = window.confirm('ไม่พบการชำระเงิน?');
		if(ans == false){
			return;
		}
		var dTarget = $(event.delegateTarget);
		var id = dTarget.attr('rowId');
		$('.action', dTarget).button('loading');
		$.ajax('old.php?page=accounting/payment.rest', {
			type: 'OPTIONS',
			data: {id: id, operation: 'paidMiss'},
			dataType: 'JSON',
			success: function(data){
				$('.action', dTarget).button('reset');
				if(data.success){
					delRow(dTarget);
				}
			}
		});
	})
	.delegate('.paidNotFully', 'click', function(event){
		event.preventDefault();
		var modal = $($('.modalPaidMiss').html());
		modal.modal();
		modal.on('hidden', function(){
			modal.remove();
		});
		var dTarget = $(event.delegateTarget);
		var id = dTarget.attr('rowId');
		$('.cancel', modal).click(function(){ $('.close', modal).click(); });
		$('.submit', modal).click(function(){
			$('.submit', modal).button('loading');
			$.ajax('old.php?page=accounting/payment.rest', {
				type: 'OPTIONS',
				data: {id: id, operation: 'paidNotFully', payTrue: $('.payTrue', modal).val(), moneyMiss: $('.moneyMiss', modal).val(), info: $('.info', modal).val()},
				dataType: 'JSON',
				success: function(data){
					$('.submit', modal).button('reset');
					if(data.success){
						delRow(dTarget);
                        modal.modal('hide');
					}
				}
			});
		});
	});
	
	var oTable = $('.dataTable').dataTable({"bFilter": false, "bInfo": false, "bSort": false,
		sDom: "<'pull-right'l>t<'row-fluid'<'span6'f><'span6'p>>",
        sPaginationType: "bootstrap",
        oLanguage: {
            "sLengthMenu": "Show _MENU_ entries"
        }
    });

	function delRow(row){
		$(row).fadeOut(function(){
			row = $(row).get(0);
			oTable.fnDeleteRow(oTable.fnGetPosition(row));
		});
	}
});
</script>